from datetime import datetime, date
from typing import Dict, List
import threading

class StatusManager:
    _instance = None
    _lock = threading.Lock()
    
    def __new__(cls):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super(StatusManager, cls).__new__(cls)
                cls._instance._init_data()
            return cls._instance
    
    def _init_data(self):
        """初始化或重置数据"""
        self.current_date = date.today()
        self.stats = {
            'content_fetch': {
                'count': 0,
                'last_error': None,
                'error_time': None
            },
            'llm_judge': {
                'count': 0,
                'last_error': None,
                'error_time': None
            },
            'tts_generation': {
                'count': 0,
                'last_error': None,
                'error_time': None
            }
        }
    
    def _check_date(self):
        """检查是否需要重置数据（新的一天）"""
        current = date.today()
        if current != self.current_date:
            self._init_data()
            self.current_date = current
    
    def increment_count(self, step: str):
        """增加指定步骤的处理计数"""
        with self._lock:
            self._check_date()
            if step in self.stats:
                self.stats[step]['count'] += 1
    
    def record_error(self, step: str, error_msg: str):
        """记录指定步骤的错误信息"""
        with self._lock:
            self._check_date()
            if step in self.stats:
                self.stats[step]['last_error'] = error_msg
                self.stats[step]['error_time'] = datetime.now()
    
    def get_status(self) -> Dict:
        """获取当前状态"""
        with self._lock:
            self._check_date()
            return {
                'date': self.current_date.strftime('%Y-%m-%d'),
                'stats': self.stats
            }

# 全局单例实例
status_manager = StatusManager() 